package xyz.example.securegeyserext.command

import org.geysermc.geyser.api.command.Command
import org.geysermc.geyser.api.command.CommandSource
import org.geysermc.geyser.api.connection.GeyserConnection
import xyz.example.securegeyserext.SecureBridgeExt

object SecureCommand {
    
    fun createCommand(): Command {
        return Command.builder<CommandSource>(SecureBridgeExt.getInstance())
            .name("securebridge")
            .bedrockOnly(true)
            .source(CommandSource::class.java)
            .aliases(listOf("sb", "secure"))
            .description("Send an encrypted message through the secure bridge")
            .executableOnConsole(false)
            .suggestedOpOnly(false)
            .permission("securebridge.use")
            .executor { source: CommandSource, _: Command, args: Array<String> ->
                executeSecureCommand(source, args)
            }
            .build()
    }
    
    private fun executeSecureCommand(source: CommandSource, args: Array<String>) {
        if (source !is GeyserConnection) {
            source.sendMessage("This command can only be used by Bedrock players!")
            return
        }

        if (args.isEmpty()) {
            source.sendMessage("Usage: /securebridge <message>")
            return
        }

        val message = args.joinToString(" ")
        
        try {
            // 发送加密消息到服务器
            SecureBridgeExt.getInstance().sendSecureMessage(source, message)
            source.sendMessage("Secure message prepared: $message")
        } catch (ex: Exception) {
            source.sendMessage("Failed to prepare secure message: ${ex.message}")
            SecureBridgeExt.getInstance().logger().error("Failed to prepare secure message", ex)
        }
    }
} 